home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 10-InfoSystems / samba / samba-share.frm.z / samba-share.frm
Encoding:
Text File  |  1997-07-30  |  9.9 KB  |  374 lines

  1. #!/usr/bin/perl5
  2. #
  3. # samba-share.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: samba-share.frm,v 1.7 1997/06/19 22:26:35 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $conf = "/usr/local/samba/lib/smb.conf";
  26. $dummy = "/usr/local/samba/lib/smb.conf.tmp";
  27. $myname = "samba-share.cgi";
  28. $title = "SAMBA Shares";
  29.  
  30. $js_main = "
  31. type = \"share\";
  32. $js_account_main;
  33. $js_error_box;
  34. $js_filename;
  35. function testAdd(form) {
  36.     if (form.new_account.value.length == 0) {
  37.         errorBox(form.new_account, \"To create a new share, enter\\n\"
  38.             + \"the new share name and click\\n\"
  39.             + \"the add button.\");
  40.         return (false);
  41.     }
  42.     if (!testSambaName(form.new_account)) return (false);
  43.     return (true);
  44. }
  45. function testSambaName(Ctrl) {
  46.     if (!testFileChars(Ctrl.value)) {
  47.         errorBox(Ctrl, \"The share name cannot contain meta \"
  48.             + \"characters.\");
  49.         return (false);return (false);
  50.     }
  51.     word = Ctrl.value;
  52.     if (word.indexOf(' ', 0) != -1 || word.indexOf('\t', 0) != -1) {
  53.         errorBox(Ctrl, \"The share name cannot contain whitespace \"
  54.                 + \"characters.\");
  55.         return (false);
  56.     }
  57.     return (true);
  58. }";
  59.  
  60. $js_edit = 
  61. "$js_standard;
  62. $js_error_box;
  63. $js_filename;
  64. function checkForm(form) {
  65.     if (form.new_name && !testSambaName(form.new_name)) return (false);
  66.     if (form.comment.value.length > 0 && !testFileChars(form.comment.value)) {
  67.         errorBox(form.comment, \"The comment cannot contain \"
  68.             + \"meta characters.\");
  69.         return (false);
  70.     }
  71.     if (form.path.value.length == 0) return (true);
  72.     if (!testFilename(form.path, \"path\")) return (false);
  73.     return (true);
  74. }
  75. function testSambaName(Ctrl) {
  76.     if (!testFileChars(Ctrl.value)) {
  77.         errorBox(Ctrl, \"The share name cannot contain meta \"
  78.             + \"characters.\");
  79.         return (false);
  80.     }
  81.     word = Ctrl.value;
  82.     if (word.indexOf(' ', 0) != -1 || word.indexOf('\t', 0) != -1) {
  83.         errorBox(Ctrl, \"The share name cannot contain whitespace \"
  84.                 + \"characters.\");
  85.         return (false);
  86.     }
  87.     return (true);
  88. }";
  89.  
  90.  
  91. print "Content-type: text/html\n\n";
  92.  
  93. if ( ! -e "/usr/etc/ppp" ) {
  94.     &title_block($title);
  95.     &header_block($title);
  96.     print "<i>SAMBA software not installed.  Install subsystem
  97.         </i><b>fw_samba.sw.base</b><i> from the freeware CD.</i>";
  98.     exit 0;
  99. }
  100.  
  101. &get_fields;
  102.  
  103. &getAccounts;
  104.  
  105. if (%fld) {
  106.         $fld{'chosen'} =~ /([\w.-]+)/;
  107.     $fld{'chosen'} = $1;
  108.  
  109.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  110.     $help =~ s/cgi$/hlp/;
  111.     exec $help if ($fld{'help'} eq "Help");
  112.  
  113.     if ($fld{'add'}) {
  114.             &error(2,"Share name required.") if !$fld{'new_account'};
  115.             &error(2,"Invalid share name.") 
  116.             if $fld{'new_account'} =~ /$METACHARS/o || $fld{'new_account'} =~ /\s/; 
  117.             $fld{'chosen'} = $fld{'new_account'}; 
  118.         $fld{'mode'} = "add";
  119.         $val{'public'} = "No";
  120.         $val{'writeable'} = "No";
  121.         $val{'printable'} = "No";
  122.  
  123.             &editAccount; 
  124.     }
  125.         elsif ($fld{'delete'}) {
  126.             &error(2,"To delete an existing share, first select a share from "
  127.                     . "list, then click the delete button.") if !$fld{'chosen'};
  128.         $message = qq|Click "Ok" to save changes.|;
  129.         &generic($fld{'chosen'}); 
  130.     }
  131.     elsif ($fld{'edit'}) {
  132.         &error(2,"To edit an existing share, first select a share from "
  133.             . "list, then click the edit button.") if !$fld{'chosen'};
  134.         &get_vals($fld{'chosen'});
  135.         $fld{'mode'} = "edit";
  136.         &editAccount; 
  137.     }
  138.     elsif ($fld{'doedit'}) { 
  139.         if ($fld{'mode'} eq "add") { &doAdd; }
  140.         else { &doEdit; } 
  141.         &restart;
  142.         &getAccounts; 
  143.         &generic; 
  144.     }
  145.         elsif ($fld{'doit'}) { &tryToDelete; &restart; &getAccounts; &generic; }
  146.         else { $message = "Use buttons to submit form."; $val{'new_account'} = $fld{'new_account'}; &generic; }
  147. } else { &generic; }
  148.  
  149. sub error {
  150.     &error_block($_[1]);
  151.     %val = %fld;
  152.     if ($_[0] == 0) { &addAccount; }
  153.     elsif ($_[0] == 1) { &editAccount; }
  154.     else { &generic; }
  155.     exit 0;
  156. }
  157.  
  158. sub restart {
  159.     $chk = system ("/etc/chkconfig", "samba");
  160.     if (! $chk) {
  161.         system("/etc/init.d/samba stop > /dev/null 2>&1");
  162.         system("/etc/init.d/samba start > /dev/null 2>&1");
  163.     }
  164. }
  165.  
  166. sub doEdit {
  167.        open(IN, "< $conf");
  168.     open(OUT, "> $dummy");
  169.  
  170.     while(<IN>) {
  171.         if ($_ =~ /\[$fld{'chosen'}\]/) { 
  172.             print OUT "[" . $fld{'new_name'} . "]\n";
  173.             
  174.             &print_share;
  175.  
  176.             $done = 0;
  177.             while(!$done) {
  178.                 $in = <IN>;
  179.                 if ($in eq "" || $in =~ /\[.*\]/) {
  180.                     $done = 1;
  181.                     print OUT $in;
  182.                 }
  183.             }
  184.         } else { print OUT $_; }
  185.     }
  186.     close(IN);
  187.     close(OUT);
  188.     rename($dummy, $conf);
  189.      
  190.     $message = "Share edited.";
  191. }    
  192.  
  193. sub print_share {
  194.     if ($fld{'comment'}) { print OUT "   comment = $fld{'comment'}\n"; }
  195.     if ($fld{'path'}) { print OUT "   path = $fld{'path'}\n"; }
  196.     if ($fld{'public'} eq "Yes") { print OUT "   public = yes\n"; }
  197.     if ($fld{'writeable'} eq "Yes") { print OUT "   writeable = yes\n"; }
  198.     if ($fld{'printable'} eq "Yes") { print OUT "   printable = yes\n"; }
  199.     print OUT "\n"; 
  200. }
  201.  
  202. sub get_vals {
  203.     $val{'public'} = "No";
  204.     $val{'writeable'} = "No";
  205.     $val{'printable'} = "No";
  206.  
  207.     open(IN, "< $conf");
  208.     $read_lines = 0;
  209.     while(<IN>) {
  210.         if ($_ =~ /\[$fld{'chosen'}\]/) { $read_lines = 1; next; }
  211.         elsif ($_ =~ /\[.*\]/) { $read_lines = 0; next; }
  212.         next if $read_lines == 0;
  213.         next if substr($_,0,1) eq ";";
  214.     
  215.         if ($_ =~ /(.*)=(.*)/) {
  216.             $parm = $1;
  217.             $value = $2;    
  218.  
  219.             $parm =~ /^\s*([^\s].*[^\s])\s*$/; $parm = $1;
  220.             $value =~ /^\s*([^\s].*)$/; $value = $1;
  221.  
  222.             if ($parm eq "public" && $value eq "yes") { $val{'public'} = "Yes"; }
  223.             if ($parm eq "writeable" && $value eq "yes") { $val{'writeable'} = "Yes"; }
  224.             if ($parm eq "read only" && $value eq "no") { $val{'writeable'} = "Yes"; }
  225.             if ($parm eq "printable" && $value eq "yes") { $val{'printable'} = "Yes"; }
  226.  
  227.             if ($parm eq "comment" || $parm eq "path") {
  228.                 $val{$parm} = $value;
  229.             }
  230.         }
  231.     }
  232. }
  233.  
  234. sub editAccount {
  235.         &js_title_block($title,$js_edit);
  236.  
  237.         &header_block("Edit SAMBA Share");
  238.  
  239.     print "$message<br>";
  240.         
  241.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">";
  242.  
  243.     print "<input type=hidden name='chosen' value=$fld{'chosen'}>\n"
  244.         . "<input type=hidden name='mode' value=$fld{'mode'}>\n";
  245.  
  246.     print "<center><table width=450>\n";
  247.  
  248.     if ($fld{'mode'} eq "edit") {
  249.         print "<tr><th align=left>Share name:"
  250.             . "<td>", &text("new_name", $fld{'chosen'}, 20), "</td></tr>\n";
  251.     } else {
  252.         print "<tr><th align=left>Share name:"
  253.             . "<td><tt>$fld{'chosen'}</tt></td></tr>\n";
  254.     }
  255.     
  256.     print "<tr><th align=left>Comment for share:"
  257.         . "<td>", &text("comment", $val{'comment'}, 20), "</td></tr>\n";
  258.  
  259.     print "<tr><th align=left>Mount path of share:"
  260.         . "<td>", &text("path", $val{'path'}, 20), "</td></tr>\n";
  261.  
  262.     print "<tr><th align=left>Allow public access?"
  263.         . "<td><b>", &radio("public", $val{'public'}, "Yes", "No"), "</b></td></tr>\n";
  264.  
  265.     print "<tr><th align=left>Allow write privileges?"
  266.         . "<td><b>", &radio("writeable", $val{'writeable'}, "Yes", "No"), "</b></td></tr>\n";
  267.  
  268.     print "<tr><th align=left>Is this a printing service share?"
  269.         . "<td><b>", &radio("printable", $val{'printable'}, "Yes", "No"), "</b></td></tr>\n";
  270.  
  271.     print "</table></center><br>\n";
  272.  
  273.         print &js_buttons('doedit','Ok','onClick="markOK()"','onClick="markOther()"');
  274.  
  275.         print "</form></body></html>";
  276. }
  277.     
  278. sub tryToDelete {
  279.     if ($fld{'del_name'} eq "") { return; }
  280.  
  281.     open(IN, "< $conf");
  282.     open(OUT, "> $dummy");
  283.     while(<IN>) {
  284.         if ($_ =~ /\[$fld{'del_name'}\]/) {
  285.             while(<IN>) {
  286.                 if ($_ =~ /\[.*\]/) { print OUT $_; last; }
  287.             }
  288.         } else { print OUT $_; }
  289.     }
  290.     close(IN);
  291.     close(OUT);
  292.     rename($dummy, $conf);
  293.  
  294.     $message = "Share deleted.";    
  295. }
  296.  
  297. sub doAdd {
  298.     open(OUT, ">> $conf");
  299.  
  300.     print OUT "\n[" . $fld{'chosen'} . "]\n";
  301.     &print_share;
  302.         
  303.     close(OUT);
  304.  
  305.     $message .= "Share added.";
  306. }
  307.  
  308. sub getAccounts {
  309.         $i = 0;
  310.         open(IN,"< $conf");
  311.         while(<IN>) {
  312.             if ($_ =~ /\[(.*)\]/) {
  313.             next if $1 eq "global";
  314.                 $name[$i++] = $1;
  315.             }
  316.         }
  317.         $number = $i;
  318.         close(IN);
  319. }
  320.  
  321. sub generic {
  322.     &js_title_block($title,$js_main);
  323.     &header_block($title);
  324.  
  325.     if (!$number && !$message) { $message = "No existing SAMBA shares."; }
  326.     print "<i>$message</i>";
  327.  
  328.     print "<form name=AccountForm action=$myname method=post onSubmit=\"return runSubmit()\">";
  329.     
  330.     if ($_[0]) { print qq|<input type=hidden name="del_name" value=$_[0]>|; }
  331.  
  332.     if ($number) {
  333.     print "<center><table width=400>";
  334.  
  335.     print qq|<tr><td><input type=submit name="add" value="Add New Share"|,
  336.         qq| onClick="markAdd()"> |,
  337.             qq|</td><td><input name="new_account" value="$val{'new_account'}" size=19></td></tr>|;
  338.  
  339.     print "<tr><td>";
  340.     print qq|<input type=submit name="edit" 
  341.         value="Edit Selected Share" onClick="markEdit()"></td>|;
  342.  
  343.     undef @locList;
  344.     for ($i=0;$i<$number;$i++) { 
  345.         if ($_[0] ne $name[$i]) { push(@locList,$name[$i]); }
  346.     }
  347.  
  348.     print "<td rowspan=2>";
  349.  
  350.     print &choice_list(*locList,"chosen",20);
  351.  
  352.     print "</td></tr>";
  353.  
  354.     print qq|<tr><td><input type=submit name="delete" 
  355.         value="Delete Selected Share" onClick="markDelete()"></td></tr>|;    
  356.  
  357.  
  358.     print "</table></center><br>";
  359.     } else {
  360.     print "<center><table width=400>";
  361.  
  362.     print qq|<tr><td><input type=submit name="add" value="Add New Share" |,
  363.     qq|onClick="markAdd()"> |,
  364.            qq|</td><td><input name="new_account" value="$val{'new_account'}" size=19></td></tr>|;
  365.  
  366.     print "<tr><td>";
  367.     print "</table>";
  368.     }
  369.  
  370.     print &js_buttons('doit','Ok','onClick="markOther()"','onClick="markOther()"');
  371.  
  372.     print "</form></body></html>";
  373. }
  374.